﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../global/head.h"
#include "../logger/head.h"
#include "helper_log.h"
#include "config.h"

namespace xos_log
{

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    helper_log::helper_log()
    {
    }

    helper_log::~helper_log()
    {
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int helper_log::proc_log( log_msg * pMsg )
    {
        int ret = 0;

        log_module * pModule = 0;
        log_level * pLevel = 0;

        if( 0 == ret )
        {
            pModule = g_pSysData->find_log_module( pMsg->m_name.c_str() );
            if( !pModule )
            {
                ret = 1;
            }
        }

        if( 0 == ret )
        {
            pLevel = pModule->find_log_level( pMsg->m_nLevel );
            if( !pLevel )
            {
                ret = 1;
            }
        }

        if( 0 == ret )
        {
            ret = fmt_log( pMsg );
        }

        for( logger::ITER i = logger::list()->begin(); ( 0 == ret ) && ( i != logger::list()->end() ); ++i )
        {
            logger * p = *i;
            p->m_pLogger->out_put_log( pMsg );
        }

        if( pMsg )
        {
            pMsg->release();
            pMsg = 0;
        }

        return ret;
    }

    int helper_log::fmt_log( log_msg * pMsg )
    {
        int ret = 0;

        xos::i_time * pTime = g_pSysData->m_pTime;

        if( 0 == ret )
        {
            pTime->get_time();
            pMsg->m_nMilliSecond = pTime->get_milli_second();
            pMsg->m_nMinute = pTime->get_minute();
            pMsg->m_nSecond = pTime->get_second();
            pMsg->m_nYear = pTime->get_year();
            pMsg->m_nMonth = pTime->get_month();
            pMsg->m_nDay = pTime->get_day();
            pMsg->m_nHour = pTime->get_hour();
        }

        if( 0 == ret )
        {
            char log[40960];
            g_pSysData->m_pContainer->crt()->sprintf( log, sizeof( log ), "%04u-%02u-%02u %02u:%02u:%02u.%03u | %-4s | %02u | %04u | %s | %s",
                pMsg->m_nYear,
                pMsg->m_nMonth,
                pMsg->m_nDay, 
                pMsg->m_nHour,
                pMsg->m_nMinute,
                pMsg->m_nSecond,
                pMsg->m_nMilliSecond,
                pMsg->m_name.c_str(),
                pMsg->m_nLevel,
                pMsg->m_nSrcCodeLine,
                pMsg->m_file.c_str(),
                pMsg->m_log.c_str() );
            pMsg->m_log = log;
        }

        return ret;
    }

} // xos_log
